package billing.rmi.impl;

import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;

import billing.rmi.inf.ICache;
import billing.system.SystemConfigCenter;
import billing.system.logger.Logger;

public class CacheImpl extends UnicastRemoteObject implements ICache {
	private static final long serialVersionUID = -1562521776249883290L;
	static Logger logger = Logger.getLogger(billing.rmi.impl.CacheImpl.class);

	protected CacheImpl() throws RemoteException {
		super();
	}

	private final static int maxLen = 1024;
	private final static Object[] valueList = new Object[maxLen];
	private final static String[] keyList = new String[maxLen];
	private static int currIndex = 0;

	@Override
	public Object get(String key) throws RemoteException {
		Object value = null;
		if (key != null && !key.equals("")) {
			for (int i = 0; i < currIndex; i++) {
				if (keyList[i].equals(key)) {
					value = valueList[i];
					break;
				}
			}
		}
		logger.info(" do get by key[" + key + "] value [" + value
				+ "] success!! ");
		return value;
	}

	@Override
	public void put(String key, Object value) throws RemoteException {
		logger.info(" do put value [" + value + "] at by key[" + key
				+ "] success!! ");
		boolean isPut = false;
		if (key != null && !key.equals("")) {
			for (int i = 0; i < currIndex; i++) {
				if (keyList[i].equals(key)) {
					valueList[i] = value;
					isPut = true;
					break;
				}
			}
			if (!isPut) {
				keyList[currIndex] = key;
				valueList[currIndex] = value;
				currIndex++;
			}
		}
	}

	@Override
	public void remove(String key) throws RemoteException {
		Object value = null;
		if (key != null && !key.equals("")) {
			for (int i = 0; i < currIndex; i++) {
				if (keyList[i].equals(key)) {
					value = valueList[i];
					valueList[i] = null;
					keyList[i] = null;
				}
			}
		}
		logger.info(" do delete by key[" + key + "] value [" + value
				+ "] success!! ");
	}

	@Override
	public void removeAll() throws RemoteException {
		logger.info(" do delete all success !");
		currIndex = 0;
	}

	public static void main(String[] args) {
		Exception exception = null;
		try {
			ICache icache = new CacheImpl();
			String url = SystemConfigCenter
					.get("billing.utils.rmi.MemRMI.iCache");
			LocateRegistry.createRegistry(6688);
			Naming.bind(url, icache);
			logger.info("cache start .....by url[" + url + "] ! success");
		} catch (RemoteException e) {
			logger.info("create Remote object has exception !");
			exception = e;
		} catch (AlreadyBoundException e) {
			logger.info("Repeat binding object exception !");
			exception = e;
		} catch (MalformedURLException e) {
			logger.info("The occurrence of URL abnormal!");
			exception = e;
		}
		if (null != exception) {
			exception.printStackTrace();
		}
	}

}
